<template>
    <div>
        <div @click="popup.openPopup" class="vue-flow-model">
            <n-icon size="30" color="#18A058">
                <UserMilitary/>
            </n-icon>
            <Handle id="UserStratificationModel-inlet" type="target" :position="Position.Left"
                    :is-valid-connection="props.isValidSourcePos"/>
            <Handle id="UserStratificationModel-export" type="source" :position="Position.Right"
                    :is-valid-connection="props.isValidTargetPos"/>
        </div>
        <span>用户分层模型</span>
        <flow-popup-window
            :show="popup.show"
            :on-click-outside="popup.clickPopupOutside"
            :x="popup.x"
            :y="popup.y"
        >
            <n-card title="用户分层模型">
                <div
                    v-for="(index,item) in column"
                    v-show="!(item===3)"
                    class="column"
                >
                    <div>{{ index.name }}</div>
                    <n-select
                        v-model:value="index.key"
                        :options="props.data.fatherData.title"
                        @update:value="popup.reopen"
                        placeholder="请选择字段列"
                        label-field="title"
                        value-field="key"
                    />
                </div>
                <n-table :bordered="false" :single-line="false">
                    <thead>
                        <tr>
                            <th>用户类型</th>
                            <th>用户占比</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="index in uerList">
                            <td>{{ index.name }}</td>
                            <td>{{ index.value }}</td>
                        </tr>
                    </tbody>
                </n-table>

                <template #footer>
                    <n-space>
                        <n-button @click="getSuperiorData">获取上级节点数据</n-button>
                        <n-button @click="began">预测</n-button>
                    </n-space>
                </template>
            </n-card>
        </flow-popup-window>
    </div>
</template>

<script setup>

import {Handle, Position} from "@vue-flow/core";
import {UserMilitary} from "@vicons/carbon";
import {useElementsStore} from "@/stores/elements";
import workApi from "@/http/workApi";
import FlowPopupWindow from "@/components/VueFlowModel/vueFlowPopupWindow/FlowPopupWindow.vue";

const props = defineProps(['isValidTargetPos', 'isValidSourcePos', 'id', 'data'])
const elements = useElementsStore()
// 点击弹窗
const popup = reactive({
    show: false,
    x: null,
    y: null,
    wait: false,
    clickPopupOutside: () => {
        popup.closedPopup()
    },
    openPopup: (e) => {
        popup.x = e.x
        popup.y = e.y
        popup.show = true
    },
    closedPopup: () => {
        if (!popup.wait) {
            console.log(22)
            popup.show = false;
        }
    },
    reopen: () => {
        popup.show = true
    }
})
const column = reactive([
    {
        name: "recency",
        key: null
    },
    {
        name: "frequency",
        key: null
    },
    {
        name: "monetary",
        key: null
    },
    {
        name: "user_id",
        key: "1"
    }
])
const uerList = ref([])
const began = () => {
    workApi.post("perform_tasks/user_rfm", {
        data: props.data.fatherData,
        column
    }).then(r=>{
        uerList.value = r.data
        props.data.data = {
            title:[
                {defaultSortOrder:false,key:"name",sorter:"default",title:"用户类型",type:"str"},
                {defaultSortOrder:false,key:"value",sorter:"default",title:"用户占比",type:"str"},
            ],
            data:r.data
        }
    })
}
const getSuperiorData = () => {
    // 获取上级节点数据
    for (let i of elements.elements) {
        if (i['target'] === props.id) {
            for (let j of elements.elements) {
                if (j.id === i['source']) {
                    props.data.fatherData = {...j.data.data};
                    props.data.fatherData.title.map(s => {
                        // 移除可修改
                        delete s.render
                        return s
                    })
                    return
                }
            }
        }
    }
}
</script>

<style scoped lang="less">
.n-card {
    width: 500px;

    .column {
        display: flex;

        > div {
            flex: 1;
        }

        > div:nth-child(2) {
            flex: 4;
        }
    }
}
</style>